﻿using D.B.Data.API.Applications.Command;
using D.B.Data.API.Applications.Command.RoleCommand;
using D.B.Data.Domain;
using D.B.Data.Domain.DTO.Input;
using D.B.Data.Domain.DTO.Output;
using D.B.Data.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using System.Security.Claims;

namespace D.B.Data.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator mediator;

        public ManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Task<APIResult<LoginDTO>> Loing([FromQuery] LoginCommand login)
        {

            return mediator.Send(login);
        }


        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<AddressModel>>> GetAddress([FromQuery] QuerAdderssCommand quer)
        {
            return await mediator.Send(quer);
        }


        /// <summary>
        /// 获取有用户信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIPaging<UserDTO>> GetUserInfo([FromQuery] QueryUserCommand command)
        {
            return  mediator.Send(command);
        }


        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> AddUser(AddUserCommand command)
        {
            command.UserId =Convert.ToInt64(HttpContext.User.FindFirstValue("Id"));
            return mediator.Send(command);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<object>> DelUser(long Id)
        {
            DelUserCommand command=new DelUserCommand();
            command.Id = Id;
            return mediator.Send(command);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="uPdate"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> UPdateUser(UPdateUserCommand uPdate)
        {
            return mediator.Send(uPdate);
        }


        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<object>> AddRole(AddRoleCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIPaging<QueryRoleDTO>> QueryRole([FromQuery]QuerRoleCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<object>> DelRole([FromQuery]DelRoleCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<object>> UpRoel(UpDataRoleCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 通过部门Id 获取用户信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<GiveDeptIdGetUserDTO>>> GiveDeptGetUser([FromQuery] GiveDeptIdQueryUserCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 用户分配角色
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<object>> UserGiveRole(AddRoleUserCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 反填角色对应的用户id
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<GiveRoleGetUserOutput>>> GetUserId([FromQuery]GiveRoleGetUserCommand command)
        {
            return mediator.Send(command);
        }


        /// <summary>
        /// 删除角色用户中间表 
        /// </summary>
        /// <param name="command">参数</param>
        /// <returns></returns>

        [HttpPost]

         public Task<APIResult<int>> DelUserRole([FromQuery]DelUserRoleCommand command)
        {
            return mediator.Send(command);
        }
    }
}
